<?php
class CompaniesController extends AppController{

	var $uses = array('Company');

	function beforeFilter(){
		parent::beforeFilter();
	}

	/*
	 * standard method
	 */
	public function index(){
		$params = array();
		if(!empty($this->params['form'])
			&& (strlen($this->params['form']['find_company_name'])>0
			 || strlen($this->params['form']['find_city'])>0)
		){
			$params = array(
				'conditions' => array(
					'company_name LIKE' => '%'.$this->params['form']['find_company_name'].'%',
					'city LIKE' => '%'.$this->params['form']['find_city'].'%'
			)
			);
		}else{
			$this->params['form']['find_company_name']='';
			$this->params['form']['find_city']='';
		}
		$this->data = $this->Company->find('all',$params);
		if(empty($this->data)){
			parent::doFlash(array('Info'=>'Es sind keine Unternehmen vorhanden.'));
		}
		
	}

	function add(){
		if(!empty($this->data)){
			$ok = $this->Company->save($this->data);
			$trueMsg = 'Das Unternehmen wurde angelegt';
			$falseMsg = 'Das Unternhemen konnte nicht angelegt werden. '. $this->Session->error();
			parent::doFlash($ok, $trueMsg, $falseMsg);
			if($ok){
				$this->redirect('/manager/companies/index');
			}
		}
		$ref = $this->referer();
		
	}

	function update($id=null){
		if(is_numeric($id)){
			if(!empty($this->data)){
				$this->Company->id = $id;
				$ok = $this->Company->save($this->data);
				$name = $this->data['Company']['company_name'];
				$trueMessage = 'Das Unternehmen "'.$name.'" wurde geändert';
				$falseMessage = 'Das Unternehmen "'.$name.'" konnte nicht geändert werden. '.$this->Session->error();
				parent::doFlash($ok, $trueMessage, $falseMessage);
				if($ok){
					$this->redirect(array('controller'=>'companies','action'=>'index'));
				}
			}
			$this->data = $this->Company->findById($id);
		}
	}
	
	function delete($id=null){
		
		if(is_numeric($id)){
			$result = $this->Company->findById($id);
			
			$dependencies = array();
			// check notices
			if(!empty($result['Notice'])){
				$dependencies[] = sizeof($result['Notice']).' Meldung(en)';
			}
			// check students
			if(!empty($result['Student'])){
				$dependencies[] = sizeof($result['Student']).' Student(en)';
			}
			// check contactPersons
			if(!empty($result['ContactPersons'])){
				$dependencies[] = sizeof($result['ContactPersons']).' Ansprechpartner';
			}
			
			$ok = sizeof($dependencies)==0;
			if($ok){
				$this->Company->delete($id);	
			} 
			
			$falsemessage = 'Das Unternehmen "'.$result['Company']['company_name'].'"'; 
			$falsemessage .= ' hat Abh&auml;ngigkeiten und kann nicht gel&ouml;scht werden.';
			if(!$ok){
				$falsemessage .= " <br>Abh&auml;ngigkeiten: ";
				$falsemessage .= implode(', ', $dependencies);
			}
			$truemessage = 'Das Unternehmen "'.$result['Company']['company_name'].'"'; 
			$truemessage .= ' wurde gelöscht.';
			parent::doFlash($ok, $truemessage, $falsemessage);
			
			$this->redirect(array('controller'=>'companies','action'=>'index'));
		}
		
	}
	
	function notices($companyId){
		if(!is_null($companyId) && is_numeric($companyId)){
			$this->data = $this->Company->findById($companyId);
		}else{
			$this->redirect($this->referer());
		}
		
	}

}
?>
